package com.lineage.server.clientpackets;

import com.lineage.config.ConfigOther;
import com.lineage.data.event.ShopXSet;
import com.lineage.data.npc.shop.Npc_ShopX;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.Instance.character.CharacterPunishInstance;
import com.lineage.server.Shutdown;
import com.lineage.server.datatables.ItemRestrictionsTable;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.datatables.ItemUpdateTable;
import com.lineage.server.datatables.RecordTable;
import com.lineage.server.datatables.ShopTable;
import com.lineage.server.datatables.ShopXTable;
import com.lineage.server.datatables.lock.CharItemsReading;
import com.lineage.server.datatables.lock.DwarfForChaReading;
import com.lineage.server.datatables.lock.DwarfForClanReading;
import com.lineage.server.datatables.lock.DwarfForElfReading;
import com.lineage.server.datatables.lock.OtherUserBuyReading;
import com.lineage.server.datatables.lock.OtherUserSellReading;
import com.lineage.server.datatables.sql.ServerGmCommandTable;
import com.lineage.server.model.Instance.L1DeInstance;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1PetInstance;
import com.lineage.server.model.L1Clan;
import com.lineage.server.model.L1Inventory;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.shop.L1Shop;
import com.lineage.server.model.shop.L1ShopBuyOrderList;
import com.lineage.server.model.shop.L1ShopSellOrderList;
import com.lineage.server.serverpackets.S_CloseList;
import com.lineage.server.serverpackets.S_CnsSell;
import com.lineage.server.serverpackets.S_Disconnect;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import com.lineage.server.templates.L1ItemUpdate;
import com.lineage.server.templates.L1PrivateShopBuyList;
import com.lineage.server.templates.L1PrivateShopSellList;
import com.lineage.server.timecontroller.event.GamblingTime;
import com.lineage.server.timecontroller.server.ServerRestartTimer;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldClan;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_Result extends ClientBasePacket {
  public static final Log _log = LogFactory.getLog(C_Result.class);
  
  public static final Random _random = new Random();
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      int level;
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      if (pc.isGhost())
        return; 
      if (pc.isDead())
        return; 
      if (pc.isTeleport())
        return; 
      if (pc.isPrivateShop())
        return; 
      if (World.get().checkPcCountForAccount(pc.getAccountName()) > 1) {
        pc.sendPackets((ServerBasePacket)new S_Disconnect());
        pc.getNetConnection().kick();
        return;
      } 
      int npcObjectId = readD();
      int resultType = readC();
      int size = readC();
      int unknown = readC();
      int npcId = 0;
      boolean isPrivateShop = false;
      L1Object findObject = World.get().findObject(npcObjectId);
      if (findObject != null) {
        boolean isStop = false;
        if (findObject instanceof L1NpcInstance) {
          L1NpcInstance targetNpc = (L1NpcInstance)findObject;
          npcId = targetNpc.getNpcTemplate().get_npcId();
          isStop = true;
        } else if (findObject instanceof L1PcInstance) {
          isPrivateShop = true;
          isStop = true;
        } 
        if (isStop) {
          int diffLocX = Math.abs(pc.getX() - findObject.getX());
          Math.abs(pc.getY() - findObject.getY());
        } 
      } 
      switch (resultType) {
        case 0:
          if (size <= 0)
            break; 
          if (findObject instanceof com.lineage.server.model.Instance.L1MerchantInstance) {
            switch (npcId) {
              case 70535:
                mode_shopS(pc, size);
                break;
              case 210109:
                mode_shopS(pc, size);
                break;
              default:
                mode_buy(pc, npcId, size);
                break;
            } 
            return;
          } 
          if (findObject instanceof com.lineage.server.model.Instance.L1GamblingInstance) {
            mode_gambling(pc, npcId, size, true);
            return;
          } 
          if (findObject instanceof com.lineage.server.model.Instance.L1CnInstance) {
            mode_cn_buy(pc, size);
            return;
          } 
          if (findObject instanceof L1DeInstance) {
            L1DeInstance de = (L1DeInstance)findObject;
            mode_buyde(pc, de, size);
            return;
          } 
          if (pc.equals(findObject)) {
            mode_cn_buy(pc, size);
            return;
          } 
          if (findObject instanceof L1PcInstance && isPrivateShop) {
            L1PcInstance targetPc = (L1PcInstance)findObject;
            mode_buypc(pc, targetPc, size);
            return;
          } 
          break;
        case 1:
          if (size <= 0)
            break; 
          if (findObject instanceof com.lineage.server.model.Instance.L1MerchantInstance) {
            switch (npcId) {
              case 70023:
                mode_sellall(pc, size);
                break;
              case 111414:
                mode_firecrystal(pc, size);
                break;
              default:
                mode_sell(pc, npcId, size);
                break;
            } 
            return;
          } 
          if (findObject instanceof com.lineage.server.model.Instance.L1CnInstance) {
            mode_cn_sell(pc, size);
            return;
          } 
          if (findObject instanceof com.lineage.server.model.Instance.L1GamblingInstance) {
            mode_gambling(pc, npcId, size, false);
            return;
          } 
          if (findObject instanceof L1DeInstance) {
            if (findObject instanceof L1DeInstance) {
              L1DeInstance de = (L1DeInstance)findObject;
              mode_sellde(pc, de, size);
            } 
            return;
          } 
          if (findObject instanceof L1PcInstance && isPrivateShop) {
            L1PcInstance targetPc = (L1PcInstance)findObject;
            mode_sellpc(pc, targetPc, size);
          } 
          break;
        case 2:
          if (size <= 0 || !(findObject instanceof com.lineage.server.model.Instance.L1DwarfInstance))
            break; 
          level = pc.getLevel();
          if (level >= ConfigOther.warehouselevel) {
            mode_warehouse_in(pc, npcId, size);
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("等级低于" + ConfigOther.warehouselevel + "级无法使用帐号仓库"));
          break;
        case 3:
          if (size <= 0 || !(findObject instanceof com.lineage.server.model.Instance.L1DwarfInstance))
            break; 
          level = pc.getLevel();
          if (level >= 5)
            mode_warehouse_out(pc, npcId, size); 
          break;
        case 4:
          if (size <= 0 || !(findObject instanceof com.lineage.server.model.Instance.L1DwarfInstance))
            break; 
          level = pc.getLevel();
          if (level >= ConfigOther.warehouselevel) {
            mode_warehouse_clan_in(pc, npcId, size);
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("等级低于" + ConfigOther.warehouselevel + "级无法使用血盟仓库"));
          break;
        case 5:
          if (size > 0) {
            if (findObject instanceof com.lineage.server.model.Instance.L1DwarfInstance) {
              level = pc.getLevel();
              if (level >= 5)
                mode_warehouse_clan_out(pc, npcId, size); 
            } 
          } else {
            L1Clan clan = WorldClan.get().getClan(pc.getClanname());
            if (clan != null)
              clan.setWarehouseUsingChar(0); 
          } 
        case 6:
          return;
        case 8:
          if (size <= 0 || !(findObject instanceof com.lineage.server.model.Instance.L1DwarfInstance))
            break; 
          level = pc.getLevel();
          if (level >= ConfigOther.warehouselevel && pc.isElf()) {
            mode_warehouse_elf_in(pc, npcId, size);
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("等级低于" + ConfigOther.warehouselevel + "级无法使用妖精仓库"));
          break;
        case 9:
          if (size <= 0 || !(findObject instanceof com.lineage.server.model.Instance.L1DwarfInstance))
            break; 
          level = pc.getLevel();
          if (level >= 5 && pc.isElf())
            mode_warehouse_elf_out(pc, npcId, size); 
          break;
        case 10:
          if (size > 0)
            switch (npcId) {
              case 111410:
                mode_update_item(pc, size, npcObjectId);
                return;
            }  
        case 12:
          if (size > 0)
            switch (npcId) {
              case 70535:
                mode_shop_item(pc, size, npcObjectId);
                break;
            }  
          break;
        case 17:
          if (size <= 0 || !(findObject instanceof com.lineage.server.model.Instance.L1DwarfInstance))
            break; 
          level = pc.getLevel();
          if (pc.getInventory().checkItem(95313)) {
            if (level >= ConfigOther.warehouselevel) {
              mode_warehouse_cha_in(pc, npcId, size);
              break;
            } 
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("等级低于" + ConfigOther.warehouselevel + "级无法使用专属仓库"));
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\aE你没有钥匙无法使用专属仓库"));
          break;
        case 18:
          if (size <= 0 || !(findObject instanceof com.lineage.server.model.Instance.L1DwarfInstance))
            break; 
          level = pc.getLevel();
          if (level >= 5)
            mode_warehouse_cha_out(pc, npcId, size); 
          break;
      } 
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  private void mode_update_item(L1PcInstance pc, int size, int npcObjectId) {
    try {
      if (size != 1) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR你只能选取一样装备用来升级。"));
        pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
        return;
      } 
      int orderId = readD();
      int count = Math.max(0, readD());
      if (count != 1) {
        pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
        return;
      } 
      L1ItemInstance item = pc.getInventory().getItem(orderId);
      ArrayList<L1ItemUpdate> items = ItemUpdateTable.get().get(item.getItemId());
      String[] names = new String[items.size()];
      int index = 0;
      while (index < items.size()) {
        int toid = ((L1ItemUpdate)items.get(index)).get_toid();
        L1Item tgitem = ItemTable.get().getTemplate(toid);
        if (tgitem != null)
          names[index] = tgitem.getName(); 
        index++;
      } 
      pc.set_mode_id(orderId);
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npcObjectId, "y_up_i1", names));
    } catch (Exception e) {
      _log.error("升级装备物品数据异常: " + pc.getName());
    } 
  }
  
	/**
	 * 托售管理员(购买物品)
	 * 
	 * @param pc
	 * @param size
	 */
	private void mode_shopS(L1PcInstance pc, int size) {
		try {
			final Map<Integer, Integer> sellScoreMapMap = new HashMap<Integer, Integer>();
			for (int i = 0; i < size; i++) {
				final int orderId = this.readD();
				final int count = Math.max(0, this.readD());// 数量
				if (count <= 0) {
					_log.error("要求列表物品取得传回数量小于等于0: " + pc.getName() + ":" + (pc.getNetConnection().kick()));
					continue;
				}
				sellScoreMapMap.put(new Integer(orderId), new Integer(count));

			}
			pc.get_otherList().get_buyCnS(sellScoreMapMap);

		} catch (Exception e) {
			_log.error("购买人物托售物品数据异常: " + pc.getName());
		}
	}

	/**
	 * 托售管理员(托售物品)
	 * 
	 * @param pc
	 * @param size
	 * @param npcObjectId
	 */
	private void mode_shop_item(L1PcInstance pc, int size, int npcObjectId) {
		try {
			if (size == 1) {
				final int objid = this.readD();
				final L1Object object = pc.getInventory().getItem(objid);

				boolean isError = false;
				if (object instanceof L1ItemInstance) {
					final L1ItemInstance item = (L1ItemInstance) object;
					if (!item.getItem().isTradable()) {// 不可转移
						isError = true;
					}
					if (item.isEquipped()) {// 使用中物件
						isError = true;
					}
					if (!item.isIdentified()) {// 未鉴定物品
						isError = true;
					}
					if (item.getItem().getMaxUseTime() != 0) {// 具有时间限制
						isError = true;
					}
					if (item.get_time() != null) {// 具有存在时间
						isError = true;
					}
					if (ShopXTable.get().getTemplate(item.getItem().getItemId()) != null) {// 不可托售物品
						isError = true;
					}
					// 宠物
					final Object[] petlist = pc.getPetList().values().toArray();
					for (final Object petObject : petlist) {
						if (petObject instanceof L1PetInstance) {
							final L1PetInstance pet = (L1PetInstance) petObject;
							if (item.getId() == pet.getItemObjId()) {
								isError = true;
							}
						}
					}
					// 取回娃娃
					if (pc.getDoll(item.getId()) != null) {
						isError = true;
					}
					// 取回娃娃
					if (pc.getDoll2(item.getId()) != null) {
						isError = true;
					}

					// 超级娃娃
					if (pc.get_power_doll() != null) {
						if (pc.get_power_doll().getItemObjId() == item.getId()) {
							isError = true;
						}
					}

					if (item.getraceGamNo() != null) {// 赌狗票
						isError = true;
					}
					if (item.getEnchantLevel() < 0) {// 强化为负值
						isError = true;
					}
					if (item.getItem().getMaxChargeCount() != 0) {// 具有次数
						if (item.getChargeCount() <= 0) {// 已无次数
							isError = true;
						}
					}
					if (ItemRestrictionsTable.RESTRICTIONS.contains(item.getItemId())) {// 限制转移物品
						isError = true;
					}
					if (ServerGmCommandTable.tradeControl.contains(item.getId())) {// 限制转移物品
						pc.sendPackets(new S_ServerMessage("奖励物品无法转移"));
						return;
					}

					if (isError) {
						pc.sendPackets(new S_NPCTalkReturn(npcObjectId, "y_x_e1"));

					} else {
						// 取回天币数量
						//final L1ItemInstance itemT = pc.getInventory().checkItemX(40308, ShopXSet.ADENA);
						final L1ItemInstance itemT = pc.getInventory().checkItemX(ShopXSet.ITEMID, ShopXSet.ADENA);
						if (itemT == null) {
							// 337：\f1%0不足%s。 0_o"
							//pc.sendPackets(new S_ServerMessage(337, "天币"));
			                final L1Item shopXcash = ItemTable.get().getTemplate(ShopXSet.ITEMID);
							pc.sendPackets(new S_ServerMessage(337, shopXcash.getName()));
							// 关闭对话窗
							pc.sendPackets(new S_CloseList(pc.getId()));
							return;
						}
						// 暂存物件讯息
						pc.get_other().set_item(item);
						pc.sendPackets(new S_CnsSell(npcObjectId, "y_x_3", "ma"));
					}
				}

			} else {
				pc.sendPackets(new S_NPCTalkReturn(npcObjectId, "y_x_e"));
			}

		} catch (Exception e) {
			_log.error("人物托售物品数据异常: " + pc.getName());
		}
	}
  
  private void mode_sellall(L1PcInstance pc, int size) {
    try {
      Map<Integer, Integer> sellallMap = new HashMap<>();
      int i = 0;
      while (i < size) {
        int objid = readD();
        int count = Math.max(0, readD());
        if (count <= 0) {
          _log.error("要求列表物品取得传回数量小于等于0: " + pc.getName() + ":" + pc.getNetConnection().kick());
          return;
        } 
        if (Shutdown.isSHUTDOWN) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
          return;
        } 
        sellallMap.put(new Integer(objid), new Integer(count));
        i++;
      } 
      pc.get_otherList().sellall(sellallMap);
    } catch (Exception e) {
      _log.error("玩家卖出物品给予万物回收商数据异常: " + pc.getName());
    } 
  }
  
  private void mode_cn_buy(L1PcInstance pc, int size) {
    try {
      Map<Integer, Integer> cnMap = new HashMap<>();
      int i = 0;
      while (i < size) {
        int orderId = readD();
        int count = Math.max(0, readD());
        if (count <= 0) {
          _log.error("要求列表物品取得传回数量小于等于0: " + pc.getName() + ":" + pc.getNetConnection().kick());
          return;
        } 
        if (count > 9999) {
          _log.error("要求列表物品取得传回数量大于9999: " + pc.getName() + ":" + pc.getNetConnection().kick());
          return;
        } 
        if (Shutdown.isSHUTDOWN) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
          return;
        } 
        cnMap.put(new Integer(orderId), new Integer(count));
        i++;
      } 
      pc.get_otherList().get_buyCn(cnMap);
    } catch (Exception e) {
      _log.error("玩家买入商城物品数据异常: " + pc.getName());
    } 
  }
  
  private void mode_cn_sell(L1PcInstance pc, int size) {
    try {
      Map<Integer, Integer> cnsellMap = new HashMap<>();
      int i = 0;
      while (i < size) {
        int objid = readD();
        int count = Math.max(0, readD());
        if (count <= 0) {
          _log.error("要求列表物品取得传回数量小于等于0: " + pc.getName() + ":" + pc.getNetConnection().kick());
          return;
        } 
        if (count > 9999) {
          _log.error("要求列表物品取得传回数量大于9999: " + pc.getName() + ":" + pc.getNetConnection().kick());
          return;
        } 
        if (Shutdown.isSHUTDOWN) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
          return;
        } 
        cnsellMap.put(new Integer(objid), new Integer(count));
        i++;
      } 
      pc.get_otherList().sellcnitem(cnsellMap);
    } catch (Exception e) {
      _log.error("玩家卖出物品给予商城道具回收专员数据异常: " + pc.getName());
    } 
  }
  
  private void mode_firecrystal(L1PcInstance pc, int size) {
    try {
      Map<Integer, Integer> FCMap = new HashMap<>();
      int i = 0;
      while (i < size) {
        int objid = readD();
        int count = Math.max(0, readD());
        if (count <= 0) {
          _log.error("要求列表物品取得传回数量小于等于0: " + pc.getName() + ":" + pc.getNetConnection().kick());
          return;
        } 
        if (Shutdown.isSHUTDOWN) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
          return;
        } 
        FCMap.put(new Integer(objid), new Integer(count));
        i++;
      } 
      pc.get_otherList().sellforfirecrystal(FCMap);
    } catch (Exception e) {
      _log.error("玩家卖出物品取得火神结晶数据异常: " + pc.getName());
    } 
  }
  
  private void mode_gambling(L1PcInstance pc, int npcId, int size, boolean isShop) {
    if (isShop) {
      if (GamblingTime.isStart()) {
        pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
        return;
      } 
      Map<Integer, Integer> gamMap = new HashMap<>();
      int i = 0;
      while (i < size) {
        int orderId = readD();
        int count = Math.max(0, readD());
        if (count <= 0) {
          _log.error("要求列表物品取得传回数量小于等于0: " + pc.getName() + ":" + pc.getNetConnection().kick());
          return;
        } 
        if (Shutdown.isSHUTDOWN) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
          return;
        } 
        gamMap.put(new Integer(orderId), new Integer(count));
        i++;
      } 
      pc.get_otherList().get_buyGam(gamMap);
    } else {
      int j = 0;
      while (j < size) {
        int objid = readD();
        int count2 = Math.max(0, readD());
        if (count2 <= 0) {
          _log.error("要求列表物品取得传回数量小于等于0: " + pc.getName() + ":" + pc.getNetConnection().kick());
        } else {
          pc.get_otherList().get_sellGam(objid, count2);
        } 
        j++;
      } 
    } 
  }
  
  private void mode_warehouse_elf_out(L1PcInstance pc, int npcId, int size) {
    if (CharacterPunishInstance.get().checkCharacter(Integer.valueOf(pc.getId())).booleanValue())
      return; 
    int i = 0;
    while (i < size) {
      int objectId = readD();
      int count = Math.max(0, readD());
      if (count <= 0) {
        _log.error("要求精灵仓库取出传回数量小于等于0: " + pc.getName() + pc.getNetConnection().kick());
        return;
      } 
      L1ItemInstance item = pc.getDwarfForElfInventory().getItem(objectId);
      if (item == null) {
        _log.error("精灵仓库取出数据异常(物品为空): " + pc.getName() + "/" + pc.getNetConnection().hashCode() + 
            pc.getNetConnection().kick());
        return;
      } 
      if (!DwarfForElfReading.get().getUserItems(pc.getAccountName(), objectId, count)) {
        _log.error("精灵仓库取出数据异常(该仓库指定数据有误): " + pc.getName() + "/" + pc.getNetConnection().hashCode() + 
            pc.getNetConnection().kick());
        return;
      } 
      if (Shutdown.isSHUTDOWN) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
        return;
      } 
      if (pc.getInventory().checkDBResultItemCount(pc.getId(), item.getId(), count, 2))
        return; 
      if (!isAvailableTrade(pc, objectId, item, count))
        break; 
      if (objectId != item.getId()) {
        pc.sendPackets((ServerBasePacket)new S_Disconnect());
        break;
      } 
      if (!item.isStackable() && count != 1) {
        pc.sendPackets((ServerBasePacket)new S_Disconnect());
        break;
      } 
      if (item == null || item.getCount() < count || count <= 0)
        break; 
      if (item.getCount() <= 0L)
        break; 
      if (item.getCount() > 10000000L)
        break; 
      if (count > 10000000)
        break; 
      if (count > item.getCount())
        count = (int)item.getCount(); 
      if (pc.getInventory().checkAddItem(item, count) != 0) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(270));
        break;
      } 
      if (!pc.getInventory().consumeItem(40494, 2L)) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, "$767"));
        break;
      } 
      RecordTable.get().recordeWarehouse_elf(pc.getName(), "领取", "妖仓", item.getAllName(), count, item.getId(), 
          pc.getIp());
      pc.getDwarfForElfInventory().tradeItem(item, count, (L1Inventory)pc.getInventory());
      i++;
    } 
  }
  
  private void mode_warehouse_elf_in(L1PcInstance pc, int npcId, int size) {
    if (ServerRestartTimer.isRtartTime()) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("伺服器即将关机 无法使用"));
      return;
    } 
    if (CharacterPunishInstance.get().checkCharacter(Integer.valueOf(pc.getId())).booleanValue())
      return; 
    int i = 0;
    while (i < size) {
      int objectId = readD();
      int count = Math.max(0, readD());
      if (count <= 0) {
        _log.error("要求精灵仓库存入传回数量小于等于0: " + pc.getName() + pc.getNetConnection().kick());
        return;
      } 
      L1ItemInstance l1ItemInstance1 = pc.getInventory().getItem(objectId);
      if (l1ItemInstance1 == null) {
        _log.error("人物背包资料取出数据异常(物品为空): " + pc.getName() + "/" + pc.getNetConnection().hashCode() + 
            pc.getNetConnection().kick());
        return;
      } 
      if (!CharItemsReading.get().getUserItems(pc.getId(), objectId, count)) {
        _log.error("人物背包资料取出数据异常(该仓库指定数据有误): " + pc.getName() + "/" + pc.getNetConnection().hashCode() + 
            pc.getNetConnection().kick());
        return;
      } 
      if (Shutdown.isSHUTDOWN) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
        return;
      } 
      L1ItemInstance item = l1ItemInstance1;
      if (item.getItemId() == 40308) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("金币无法存入仓库"));
        return;
      } 
      if (item.getItemId() != 240205 && !item.getItem().isTradable()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
        return;
      } 
      if (item.get_time() != null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
        return;
      } 
      if (!isAvailableTrade(pc, objectId, item, count))
        return; 
      if (item == null || item.getCount() < count || count <= 0)
        break; 
      if (item.getCount() <= 0L)
        break; 
      if (item.getCount() > 100000000L)
        break; 
      if (count > 100000000)
        break; 
      Object[] petlist = pc.getPetList().values().toArray();
      Object[] array;
      int length = (array = petlist).length;
      int j = 0;
      while (j < length) {
        Object petObject = array[j];
        if (petObject instanceof L1PetInstance) {
          L1PetInstance pet = (L1PetInstance)petObject;
          if (item.getId() == pet.getItemObjId()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
            break;
          } 
        } 
        j++;
      } 
      if (pc.getDoll(item.getId()) != null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181));
        return;
      } 
      if (pc.get_power_doll() != null && pc.get_power_doll().getItemObjId() == item.getId()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181));
        break;
      } 
      if (pc.getDwarfForElfInventory().checkAddItemToWarehouse(item, count, 0) == 1) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(75));
        break;
      } 
      RecordTable.get().recordeWarehouse_elf(pc.getName(), "存入", "妖仓", item.getAllName(), count, item.getId(), 
          pc.getIp());
      pc.getInventory().tradeItem(objectId, count, (L1Inventory)pc.getDwarfForElfInventory());
      i++;
    } 
  }
  
  private void mode_warehouse_clan_out(L1PcInstance pc, int npcId, int size) {
    if (CharacterPunishInstance.get().checkCharacter(Integer.valueOf(pc.getId())).booleanValue())
      return; 
    L1Clan clan = WorldClan.get().getClan(pc.getClanname());
    try {
      if (clan != null) {
        int i = 0;
        while (i < size) {
          int objectId = readD();
          int count = Math.max(0, readD());
          if (count <= 0) {
            _log.error("要求血盟仓库取出传回数量小于等于0: " + pc.getName() + pc.getNetConnection().kick());
            return;
          } 
          L1ItemInstance item = clan.getDwarfForClanInventory().getItem(objectId);
          if (item == null) {
            _log.error("血盟仓库取出数据异常(物品为空): " + pc.getName() + "/" + pc.getNetConnection().hashCode() + 
                pc.getNetConnection().kick());
            return;
          } 
          if (!DwarfForClanReading.get().getUserItems(pc.getClanname(), objectId, count)) {
            _log.error("血盟仓库取出数据异常(该仓库指定数据有误): " + pc.getName() + "/" + 
                pc.getNetConnection().hashCode() + pc.getNetConnection().kick());
            return;
          } 
          if (Shutdown.isSHUTDOWN) {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
            return;
          } 
          if (clan.getWarehouseUsingChar() != pc.getId()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("血盟仓库有人正在使用中。"));
            return;
          } 
          if (pc.getInventory().checkDBResultItemCount(pc.getId(), item.getId(), count, 2))
            return; 
          if (item == null || item.getCount() < count || count <= 0)
            break; 
          if (item.getCount() <= 0L)
            break; 
          if (!isAvailableTrade(pc, objectId, item, count))
            break; 
          if (pc.getInventory().checkAddItem(item, count) != 0) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(270));
            break;
          } 
          if (!pc.getInventory().consumeItem(40308, 30L)) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(189));
            break;
          } 
          RecordTable.get().recordeWarehouse_clan(pc.getName(), "领取", "盟仓", item.getAllName(), count, 
              item.getId(), pc.getIp());
          clan.getDwarfForClanInventory().tradeItem(item, count, (L1Inventory)pc.getInventory());
          clan.getDwarfForClanInventory().writeHistory(pc, item, count, 1);
          i++;
        } 
        if (clan != null)
          clan.setWarehouseUsingChar(0); 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void mode_warehouse_clan_in(L1PcInstance pc, int npcId, int size) {
    if (ServerRestartTimer.isRtartTime()) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("伺服器即将关机 无法使用"));
      return;
    } 
    if (CharacterPunishInstance.get().checkCharacter(Integer.valueOf(pc.getId())).booleanValue())
      return; 
    L1Clan clan = WorldClan.get().getClan(pc.getClanname());
    try {
      if (clan != null) {
        int i = 0;
        while (i < size) {
          int objectId = readD();
          int count = Math.max(0, readD());
          if (count <= 0) {
            _log.error("要求血盟仓库存入传回数量小于等于0: " + pc.getName() + pc.getNetConnection().kick());
            return;
          } 
          L1ItemInstance l1ItemInstance1 = pc.getInventory().getItem(objectId);
          if (l1ItemInstance1 == null) {
            _log.error("人物背包资料取出数据异常(物品为空): " + pc.getName() + "/" + 
                pc.getNetConnection().hashCode() + pc.getNetConnection().kick());
            return;
          } 
          if (!CharItemsReading.get().getUserItems(pc.getId(), objectId, count)) {
            _log.error("人物背包资料取出数据异常(该仓库指定数据有误): " + pc.getName() + "/" + 
                pc.getNetConnection().hashCode() + pc.getNetConnection().kick());
            return;
          } 
          if (Shutdown.isSHUTDOWN) {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
            return;
          } 
          L1ItemInstance item = l1ItemInstance1;
          if (item.getItemId() == 40308) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("金币无法存入仓库"));
            return;
          } 
          if (item.getItemId() != 240205 && !item.getItem().isTradable()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
            return;
          } 
          if (item.isSeal()) {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(item.getLogName()) + "处于绑定状态！"));
            return;
          } 
          if (item.get_time() != null) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
            return;
          } 
          if (ItemRestrictionsTable.get().checkItemRestrictions(item.getItemId(), pc)) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
            return;
          } 
          if (!isAvailableTrade(pc, objectId, item, count))
            return; 
          if (item == null || item.getCount() < count || count <= 0)
            break; 
          if (item.getCount() <= 0L)
            break; 
          if (item.getCount() > 100000000L)
            break; 
          if (count > 100000000)
            break; 
          Object[] petlist = pc.getPetList().values().toArray();
          Object[] array;
          int length = (array = petlist).length;
          int j = 0;
          while (j < length) {
            Object petObject = array[j];
            if (petObject instanceof L1PetInstance) {
              L1PetInstance pet = (L1PetInstance)petObject;
              if (item.getId() == pet.getItemObjId()) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
                break;
              } 
            } 
            j++;
          } 
          if (pc.getDoll(item.getId()) != null) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181));
            return;
          } 
          if (pc.get_power_doll() != null && pc.get_power_doll().getItemObjId() == item.getId()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181));
            break;
          } 
          if (clan.getDwarfForClanInventory().checkAddItemToWarehouse(item, count, 1) == 1) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(75));
            return;
          } 
          RecordTable.get().recordeWarehouse_clan(pc.getName(), "存入", "盟仓", item.getAllName(), count, 
              item.getId(), pc.getIp());
          pc.getInventory().tradeItem(objectId, count, (L1Inventory)clan.getDwarfForClanInventory());
          clan.getDwarfForClanInventory().writeHistory(pc, item, count, 0);
          i++;
        } 
      } else {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(208));
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  private void mode_warehouse_out(L1PcInstance pc, int npcId, int size) {
    if (CharacterPunishInstance.get().checkCharacter(Integer.valueOf(pc.getId())).booleanValue())
      return; 
    int i = 0;
    while (i < size) {
      int objectId = readD();
      int count = Math.max(0, readD());
      if (count <= 0) {
        _log.error("要求帐号仓库取出传回数量小于等于0: " + pc.getName() + pc.getNetConnection().kick());
        return;
      } 
      L1ItemInstance item = pc.getDwarfInventory().getItem(objectId);
      if (item == null) {
        _log.error("帐号仓库取出数据异常(物品为空): " + pc.getName() + "/" + pc.getNetConnection().hashCode() + 
            pc.getNetConnection().kick());
        return;
      } 
      if (Shutdown.isSHUTDOWN) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
        return;
      } 
      if (pc.getInventory().checkDBResultItemCount(pc.getId(), item.getId(), count, 3))
        return; 
      if (pc.getInventory().checkDBResultItemCount(pc.getId(), item.getId(), count, 2))
        return; 
      if (item == null || item.getCount() < count || count <= 0)
        break; 
      if (item.getCount() <= 0L)
        break; 
      if (!isAvailableTrade(pc, objectId, item, count))
        break; 
      if (pc.getInventory().checkAddItem(item, count) != 0) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(270));
        break;
      } 
      if (!pc.getInventory().consumeItem(40308, 30L)) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(189));
        break;
      } 
      RecordTable.get().recordeWarehouse_pc(pc.getName(), "领取", "个仓", item.getAllName(), count, item.getId(), 
          pc.getIp());
      pc.getDwarfInventory().tradeItem(item, count, (L1Inventory)pc.getInventory());
      i++;
    } 
  }
  
  private void mode_warehouse_in(L1PcInstance pc, int npcId, int size) {
    if (ServerRestartTimer.isRtartTime()) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("伺服器即将关机 无法使用"));
      return;
    } 
    if (CharacterPunishInstance.get().checkCharacter(Integer.valueOf(pc.getId())).booleanValue())
      return; 
    int i = 0;
    while (i < size) {
      int objectId = readD();
      int count = Math.max(0, readD());
      if (count <= 0) {
        _log.error("要求帐号仓库存入传回数量小于等于0: " + pc.getName() + pc.getNetConnection().kick());
        return;
      } 
      L1ItemInstance l1ItemInstance1 = pc.getInventory().getItem(objectId);
      if (l1ItemInstance1 == null) {
        _log.error("人物背包资料取出数据异常(物品为空): " + pc.getName() + "/" + pc.getNetConnection().hashCode() + 
            pc.getNetConnection().kick());
        return;
      } 
      if (!CharItemsReading.get().getUserItems(pc.getId(), objectId, count)) {
        _log.error("人物背包资料取出数据异常(该仓库指定数据有误): " + pc.getName() + "/" + pc.getNetConnection().hashCode() + 
            pc.getNetConnection().kick());
        return;
      } 
      if (Shutdown.isSHUTDOWN) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
        return;
      } 
      L1ItemInstance item = l1ItemInstance1;
      if (item.getCount() <= 0L)
        break; 
      if (item.getItemId() == 40308) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("金币无法存入仓库"));
        return;
      } 
      if (item.isSeal()) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(item.getLogName()) + "处于绑定状态！"));
        return;
      } 
      if (!isAvailableTrade(pc, objectId, item, count))
        return; 
      if (item == null || item.getCount() < count || count <= 0)
        break; 
      if (item.getCount() <= 0L)
        break; 
      if (item.getCount() > 100000000L)
        break; 
      if (count > 100000000)
        break; 
      if (item.getItemId() != 240205 && !item.getItem().isTradable()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
        break;
      } 
      if (item.get_time() != null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
        break;
      } 
      Object[] petlist = pc.getPetList().values().toArray();
      Object[] array;
      int length = (array = petlist).length;
      int j = 0;
      while (j < length) {
        Object petObject = array[j];
        if (petObject instanceof L1PetInstance) {
          L1PetInstance pet = (L1PetInstance)petObject;
          if (item.getId() == pet.getItemObjId()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
            break;
          } 
        } 
        j++;
      } 
      if (pc.getDoll(item.getId()) != null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181));
        break;
      } 
      if (pc.get_power_doll() != null && pc.get_power_doll().getItemObjId() == item.getId()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181));
        break;
      } 
      if (pc.getDwarfInventory().checkAddItemToWarehouse(item, count, 0) == 1) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(75));
        break;
      } 
      pc.getInventory().tradeItem(objectId, count, (L1Inventory)pc.getDwarfInventory());
      RecordTable.get().recordeWarehouse_pc(pc.getName(), "存入", "个仓", item.getAllName(), count, item.getId(), 
          pc.getIp());
      i++;
    } 
  }
  
  private void mode_warehouse_cha_out(L1PcInstance pc, int npcId, int size) {
    int i = 0;
    while (i < size) {
      int objectId = readD();
      int count = Math.max(0, readD());
      if (count <= 0) {
        _log.error("要求角色专属仓库取出传回数量小于等于0: " + pc.getName() + pc.getNetConnection().kick());
        return;
      } 
      L1ItemInstance item = pc.getDwarfForChaInventory().getItem(objectId);
      if (item == null) {
        _log.error("角色专属仓库取出数据异常(物品为空): " + pc.getName() + "/" + pc.getNetConnection().hashCode() + 
            pc.getNetConnection().kick());
        return;
      } 
      if (!DwarfForChaReading.get().getUserItems(pc.getName(), objectId, count)) {
        _log.error("角色专属仓库取出数据异常(该仓库指定数据有误): " + pc.getName() + "/" + pc.getNetConnection().hashCode() + 
            pc.getNetConnection().kick());
        return;
      } 
      if (Shutdown.isSHUTDOWN) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
        return;
      } 
      if (pc.getInventory().checkDBResultItemCount(pc.getId(), item.getId(), count, 1))
        return; 
      if (item == null || item.getCount() < count || count <= 0)
        break; 
      if (item.getCount() <= 0L)
        break; 
      if (!isAvailableTrade(pc, objectId, item, count))
        break; 
      if (pc.getInventory().checkAddItem(item, count) != 0) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(270));
        break;
      } 
      if (!pc.getInventory().consumeItem(40308, 30L)) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(189));
        break;
      } 
      RecordTable.get().recordWarehouse_char_pc(pc.getName(), "领取", "角仓", item.getAllName(), count, item.getId(), 
          pc.getIp());
      pc.getDwarfForChaInventory().tradeItem(item, count, (L1Inventory)pc.getInventory());
      i++;
    } 
  }
  
  private void mode_warehouse_cha_in(L1PcInstance pc, int npcId, int size) {
    if (ServerRestartTimer.isRtartTime()) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("伺服器即将关机 无法使用"));
      return;
    } 
    int i = 0;
    while (i < size) {
      int objectId = readD();
      int count = Math.max(0, readD());
      if (count <= 0) {
        _log.error("要求角色专属仓库存入传回数量小于等于0: " + pc.getName() + pc.getNetConnection().kick());
        return;
      } 
      L1ItemInstance l1ItemInstance1 = pc.getInventory().getItem(objectId);
      if (l1ItemInstance1 == null) {
        _log.error("人物背包资料取出数据异常(物品为空): " + pc.getName() + "/" + pc.getNetConnection().hashCode());
        return;
      } 
      if (!CharItemsReading.get().getUserItems(pc.getId(), objectId, count)) {
        _log
          .error("人物背包资料取出数据异常(该人物背包指定数据有误): " + pc.getName() + "/" + pc.getNetConnection().hashCode());
        return;
      } 
      L1ItemInstance item = l1ItemInstance1;
      if (item.getCount() <= 0L)
        return; 
      if (!isAvailableTrade(pc, objectId, item, count))
        return; 
      if (item == null || item.getCount() < count || count <= 0)
        break; 
      if (item.getCount() <= 0L)
        break; 
      if (item.getCount() > 100000000L)
        break; 
      if (count > 100000000)
        break; 
      if (Shutdown.isSHUTDOWN) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
        return;
      } 
      if (item.getItemId() == 40308) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("金币无法存入仓库"));
        return;
      } 
      if (item.getItemId() != 240205 && !item.getItem().isTradable()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
        break;
      } 
      if (item.isSeal()) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(item.getLogName()) + "处于绑定状态！"));
        return;
      } 
      if (item.get_time() != null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
        break;
      } 
      Object[] petlist = pc.getPetList().values().toArray();
      Object[] array;
      int length = (array = petlist).length;
      int j = 0;
      while (j < length) {
        Object petObject = array[j];
        if (petObject instanceof L1PetInstance) {
          L1PetInstance pet = (L1PetInstance)petObject;
          if (item.getId() == pet.getItemObjId()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
            break;
          } 
        } 
        j++;
      } 
      if (pc.getDoll(item.getId()) != null) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181));
        break;
      } 
      if (pc.get_power_doll() != null && pc.get_power_doll().getItemObjId() == item.getId()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181));
        break;
      } 
      if (pc.getDwarfForChaInventory().checkAddItemToWarehouse(item, count, 0) == 1) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(75));
        break;
      } 
      RecordTable.get().recordWarehouse_char_pc(pc.getName(), "存入", "角仓", item.getAllName(), count, item.getId(), 
          pc.getIp());
      pc.getInventory().tradeItem(objectId, count, (L1Inventory)pc.getDwarfForChaInventory());
      i++;
    } 
  }
  
  private void mode_sellpc(L1PcInstance pc, L1PcInstance targetPc, int size) {
    ArrayList<L1PrivateShopBuyList> buyList = targetPc.getBuyList();
    boolean[] isRemoveFromList = new boolean[8];
    if (targetPc.isTradingInPrivateShop())
      return; 
    int adenaItemId = targetPc.getMap().isUsableShop();
    if (adenaItemId <= 0 || ItemTable.get().getTemplate(adenaItemId) == null)
      return; 
    targetPc.setTradingInPrivateShop(true);
    int i = 0;
    while (i < size) {
      int itemObjectId = readD();
      int count = readCH();
      count = Math.max(0, count);
      if (count <= 0) {
        _log.error("要求列表物品取得传回数量小于等于0: " + pc.getName() + pc.getNetConnection().kick());
        break;
      } 
      int order = readC();
      L1ItemInstance item = pc.getInventory().getItem(itemObjectId);
      if (item != null && 
        item.get_time() == null) {
        L1PrivateShopBuyList psbl = buyList.get(order);
        int buyItemObjectId = psbl.getItemObjectId();
        long buyPrice = psbl.getBuyPrice();
        int buyTotalCount = psbl.getBuyTotalCount();
        int buyCount = psbl.getBuyCount();
        if (count > buyTotalCount - buyCount)
          count = buyTotalCount - buyCount; 
        if (item.isSeal()) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(item.getLogName()) + "处于绑定状态！"));
          return;
        } 
        if (item.isEquipped()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(905));
        } else {
          L1ItemInstance srcItem = targetPc.getInventory().getItem(buyItemObjectId);
          if (srcItem.get_time() == null) {
            if (item.getItemId() != srcItem.getItemId() || 
              item.getEnchantLevel() != srcItem.getEnchantLevel()) {
              _log.error("可能使用bug进行交易 人物名称(卖出道具给予个人商店/交易条件不吻合): " + pc.getName() + " objid:" + 
                  pc.getId());
              return;
            } 
            if (targetPc.getInventory().checkAddItem(item, count) == 0) {
              int j = 0;
              while (j < count)
                j++; 
              if (!targetPc.getInventory().checkItem(adenaItemId, count * buyPrice)) {
                targetPc.sendPackets((ServerBasePacket)new S_ServerMessage(189));
                break;
              } 
              L1ItemInstance adena = targetPc.getInventory().findItemId(adenaItemId);
              if (adena == null) {
                targetPc.sendPackets((ServerBasePacket)new S_ServerMessage(904, "2000000000"));
                return;
              } 
              if (!pc.getInventory().checkDBItemCount(pc.getId(), item.getItemId(), count)) {
                targetPc.sendPackets((ServerBasePacket)new S_ServerMessage(904, "2000000000"));
                return;
              } 
              if (item.getCount() < count) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(989));
                _log.error("可能使用bug进行交易 人物名称(卖出道具给予个人商店/交易数量不吻合): " + pc.getName() + 
                    " objid:" + pc.getId());
                targetPc.sendPackets((ServerBasePacket)new S_ServerMessage(904, "2000000000"));
                return;
              } 
              OtherUserSellReading.get().add(item.getItem().getName(), item.getId(), (int)buyPrice, 
                  count, pc.getId(), pc.getName(), targetPc.getId(), targetPc.getName());
              targetPc.getInventory().tradeItem(adena, count * buyPrice, (L1Inventory)pc.getInventory());
              pc.getInventory().tradeItem(item, count, (L1Inventory)targetPc.getInventory());
              String message = String.valueOf(String.valueOf(item.getItem().getNameId())) + " (" + 
                String.valueOf(count) + ")";
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(877, targetPc.getName(), message));
              psbl.setBuyCount(count + buyCount);
              buyList.set(order, psbl);
              if (psbl.getBuyCount() == psbl.getBuyTotalCount())
                isRemoveFromList[order] = true; 
            } 
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(271));
            break;
          } 
        } 
      } 
      i++;
    } 
    i = 7;
    while (i >= 0) {
      if (isRemoveFromList[i])
        buyList.remove(i); 
      i--;
    } 
    targetPc.setTradingInPrivateShop(false);
  }
  
  private void mode_buypc(L1PcInstance pc, L1PcInstance targetPc, int size) {
    ArrayList<L1PrivateShopSellList> sellList = targetPc.getSellList();
    boolean[] isRemoveFromList = new boolean[8];
    if (targetPc.isTradingInPrivateShop())
      return; 
    int adenaItemId = targetPc.getMap().isUsableShop();
    if (adenaItemId <= 0 || ItemTable.get().getTemplate(adenaItemId) == null)
      return; 
    sellList = targetPc.getSellList();
    synchronized (sellList) {
      if (pc.getPartnersPrivateShopItemCount() != sellList.size())
        return; 
      targetPc.setTradingInPrivateShop(true);
      int i;
      for (i = 0; i < size; i++) {
        int order = readD();
        int count = Math.max(0, readD());
        if (count <= 0) {
          _log.error("要求买入个人商店物品传回数量小于等于0: " + pc.getName() + pc.getNetConnection().kick());
          break;
        } 
        if (Shutdown.isSHUTDOWN) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
          return;
        } 
        L1PrivateShopSellList pssl = sellList.get(order);
        int itemObjectId = pssl.getItemObjectId();
        int sellPrice = pssl.getSellPrice();
        int sellTotalCount = pssl.getSellTotalCount();
        int sellCount = pssl.getSellCount();
        L1ItemInstance item = targetPc.getInventory().getItem(itemObjectId);
        if (item != null)
          if (item.get_time() == null) {
            if (count > sellTotalCount - sellCount)
              count = sellTotalCount - sellCount; 
            if (count > 0)
              if (pc.getInventory().checkAddItem(item, count) == 0) {
                for (int j = 0; j < count; j++) {
                  if (sellPrice * j > 2000000000) {
                    pc.sendPackets((ServerBasePacket)new S_ServerMessage(904, "2000000000"));
                    targetPc.setTradingInPrivateShop(false);
                    return;
                  } 
                } 
                int price = count * sellPrice;
                L1ItemInstance adena = pc.getInventory().findItemId(adenaItemId);
                if (adena == null) {
                  pc.sendPackets((ServerBasePacket)new S_ServerMessage(189));
                } else if (adena.getCount() < price) {
                  pc.sendPackets((ServerBasePacket)new S_ServerMessage(189));
                } else {
                  if (adena == null) {
                    pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, 
                          ItemTable.get().getTemplate(adenaItemId).getName()));
                    pc.sendPackets((ServerBasePacket)new S_ServerMessage(904, "2000000000"));
                    targetPc.setTradingInPrivateShop(false);
                    return;
                  } 
                  if (adena.getCount() < price) {
                    pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, 
                          ItemTable.get().getTemplate(adenaItemId).getName()));
                    pc.sendPackets((ServerBasePacket)new S_ServerMessage(904, "2000000000"));
                    targetPc.setTradingInPrivateShop(false);
                    return;
                  } 
                  OtherUserBuyReading.get().add(item.getItem().getName(), item.getId(), sellPrice, count, 
                      pc.getId(), pc.getName(), targetPc.getId(), targetPc.getName());
                  targetPc.getInventory().tradeItem(item, count, (L1Inventory)pc.getInventory());
                  pc.getInventory().tradeItem(adena, price, (L1Inventory)targetPc.getInventory());
                  String message = String.valueOf(String.valueOf(item.getItem().getNameId())) + " (" + 
                    String.valueOf(count) + ")";
                  targetPc.sendPackets((ServerBasePacket)new S_ServerMessage(877, pc.getName(), message));
                  pssl.setSellCount(count + sellCount);
                  sellList.set(order, pssl);
                  if (pssl.getSellCount() == pssl.getSellTotalCount())
                    isRemoveFromList[order] = true; 
                } 
              } else {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(270));
              }  
          }  
      } 
      for (i = 7; i >= 0; i--) {
        if (isRemoveFromList[i])
          sellList.remove(i); 
      } 
      targetPc.setTradingInPrivateShop(false);
    } 
  }
  
  private void mode_sell(L1PcInstance pc, int npcId, int size) {
    L1Shop shop = ShopTable.get().get(npcId);
    if (shop != null) {
      L1ShopSellOrderList orderList = shop.newSellOrderList(pc);
      int i = 0;
      while (i < size) {
        int objid = readD();
        int count = Math.max(0, readD());
        if (count <= 0) {
          _log.error("要求列表物品取得传回数量小于等于0: " + pc.getName() + ":" + pc.getNetConnection().kick());
        } else {
          orderList.add(objid, count);
        } 
        i++;
      } 
      shop.buyItems(pc, orderList);
    } else {
      pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
    } 
  }
  
  private void mode_buy(L1PcInstance pc, int npcId, int size) {
    L1Shop shop = ShopTable.get().get(npcId);
    if (shop != null) {
      L1ShopBuyOrderList orderList = shop.newBuyOrderList();
      int i = 0;
      while (i < size) {
        int orderId = readD();
        int count = Math.max(0, readD());
        if (count <= 0) {
          _log.error("要求列表物品取得传回数量小于等于0: " + pc.getName() + ":" + pc.getNetConnection().kick());
        } else {
          orderList.add(orderId, count);
        } 
        i++;
      } 
      shop.sellItems(pc, orderList);
    } else {
      pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
    } 
  }
  
  private void mode_sellde(L1PcInstance pc, L1DeInstance de, int size) {
    Map<Integer, int[]> buyList = de.get_buyList();
    Queue<Integer> removeList = new ConcurrentLinkedQueue<>();
    int i = 0;
    while (i < size) {
      int itemObjectId = readD();
      int count = readCH();
      count = Math.max(0, count);
      if (count <= 0) {
        _log.error("要求列表物品取得传回数量小于等于0: " + pc.getName() + ":" + pc.getNetConnection().kick());
      } else {
        L1ItemInstance item = pc.getInventory().getItem(itemObjectId);
        if (item != null) {
          if (item.isSeal()) {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(item.getLogName()) + "处于绑定状态！"));
            return;
          } 
          if (item.get_time() != null) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, item.getItem().getNameId()));
          } else if (item.isEquipped()) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(905));
          } else {
            int[] sellX = buyList.get(Integer.valueOf(item.getItemId()));
            if (sellX != null) {
              int price = sellX[0];
              int enchantlvl = sellX[1];
              int buycount = sellX[2];
              if (item.getEnchantLevel() == enchantlvl && buycount >= count) {
                if (pc.getInventory().removeItem(itemObjectId, count) == count) {
                  pc.getInventory().storeItem(40308, (price * count));
                } else {
                  pc.sendPackets((ServerBasePacket)new S_ServerMessage(989));
                } 
                int newcount = buycount - count;
                if (newcount <= 0) {
                  removeList.add(Integer.valueOf(item.getItemId()));
                } else {
                  int[] newSellX = { price, enchantlvl, newcount };
                  de.updateBuyList(Integer.valueOf(item.getItemId()), newSellX);
                } 
              } 
            } 
          } 
        } 
      } 
      i++;
    } 
    if (!removeList.isEmpty()) {
      Iterator<Integer> iter = removeList.iterator();
      while (iter.hasNext()) {
        Integer reitem = iter.next();
        iter.remove();
        buyList.remove(reitem);
      } 
    } 
    (pc.get_otherList()).DELIST.clear();
  }
  
  private void mode_buyde(L1PcInstance pc, L1DeInstance de, int size) {
    Map<L1ItemInstance, Integer> sellList = de.get_sellList();
    Queue<L1ItemInstance> removeList = new ConcurrentLinkedQueue<>();
    int i = 0;
    while (i < size) {
      int order = readD();
      int count = Math.max(0, readD());
      if (count <= 0) {
        _log.error("要求买入个人商店物品传回数量小于等于0: " + pc.getName() + pc.getNetConnection().kick());
        break;
      } 
      Map<Integer, L1ItemInstance> list = (pc.get_otherList()).DELIST;
      Iterator<Integer> iterator = list.keySet().iterator();
      while (iterator.hasNext()) {
        int key = ((Integer)iterator.next()).intValue();
        if (order == key) {
          L1ItemInstance item = list.get(Integer.valueOf(key));
          if (item == null)
            continue; 
          if (item.getCount() < count)
            continue; 
          if (pc.getInventory().checkAddItem(item, count) != 0) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(270));
            break;
          } 
          long price = (count * ((Integer)sellList.get(item)).intValue());
          if (!pc.getInventory().consumeItem(40308, price)) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(189));
            break;
          } 
          if (item.isStackable()) {
            pc.getInventory().storeItem(item.getItemId(), count);
            long newCount = item.getCount() - count;
            if (newCount > 0L) {
              item.setCount(newCount);
              continue;
            } 
            removeList.add(item);
            continue;
          } 
          pc.getInventory().storeTradeItem(item);
          removeList.add(item);
        } 
      } 
      i++;
    } 
    if (!removeList.isEmpty()) {
      Iterator<L1ItemInstance> iter = removeList.iterator();
      while (iter.hasNext()) {
        L1ItemInstance reitem = iter.next();
        iter.remove();
        sellList.remove(reitem);
      } 
    } 
    (pc.get_otherList()).DELIST.clear();
  }
  
  private boolean isAvailableTrade(L1PcInstance pc, int objectId, L1ItemInstance item, int count) {
    boolean result = true;
    if (item == null)
      result = false; 
    if (objectId != item.getId())
      result = false; 
    if (!item.isStackable() && count != 1)
      result = false; 
    if (item.getCount() <= 0L || item.getCount() > 2000000000L)
      result = false; 
    if (count <= 0 || count > 2000000000)
      result = false; 
    if (!result)
      pc.sendPackets((ServerBasePacket)new S_Disconnect()); 
    return result;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
